import axios, { AxiosResponse } from "axios";
import apiConfig from "../api/Api";
const instance = axios.create({
    baseURL: 'http://192.168.0.111:7001',
    timeout: 10 * 1000,
});


instance.interceptors.request.use((config) => {

    return config;
}, (error) => {

    return Promise.reject(error);
});

/** 
 * 
 * 
 **/
instance.interceptors.response.use(
    response => response,
    error => {
        const { response } = error
        if (response) {
            const { status } = response
            if (status >= 500) {
                /* 服务端报错 */

            } else if (status === 400) {
                /* 接口参数异常 */

            } else if (status === 401) {
                /* 登录信息过期，需要重新登录 */
            }
        } else {
            /* 网络异常 */
        }
        return Promise.reject(error)
    }
)
export const request = (name: string, params: any): Promise<AxiosResponse<any, any>> => {
    const api = (apiConfig as any)[name]
    const { url, method } = api
    if (method === 'get') {
        return get(url, params)
    } else {
        return post(url, params)
    }
}


export const get = (url: string, params: any): Promise<AxiosResponse<any, any>> => {
    return instance.get(url, {
        params
    })
}

export const post = (url: string, params: any): Promise<AxiosResponse<any, any>> => {
    return instance.post(url, params)
}